package concurrent;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * TODO 简单说明
 *
 * @author yangyang
 * @date 2017/7/4 18:19
 * @email kekecany@163.com
 */
class BlockedMutex {

    private Lock lock = new ReentrantLock();

    BlockedMutex() {
        lock.lock();
    }

    void f()  {
        try {
            // 要么获取到锁，要么一直等着获取锁，要么被interrupt
            lock.lockInterruptibly();
            System.out.println("lock acquired in f()");
        }catch (InterruptedException e) {
            System.out.println("interruption from f()");
        }
    }

}

class Blocked2 implements Runnable{
    private BlockedMutex blockedMutex = new BlockedMutex();
    @Override
    public void run() {
        System.out.println("waiting for f() in BlockedMutex");
        blockedMutex.f();
        System.out.println("Broken out of blocked call");
    }
}

public class Interrupting2 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Blocked2());
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("issuing: interrupt ");
        thread.interrupt();
    }
}
